package com.java.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.java.entity.FactoryPayment;
import com.java.entity.IncomeExpenses;
import com.java.entity.OrderForm;
import com.java.entity.ReceiveExpress;

/**
 * 订单管理服务
 * @author wqf
 * @version  Nov 24, 2011 11:51:06 AM
 */
@Transactional(propagation = Propagation.REQUIRED,readOnly=false, rollbackFor = Exception.class)
public class OrderService extends BaseService{

	/**
	 * 添加
	 * @param OrderForm
	 */
	public void addOrder(OrderForm obj){
		this.dao.saveObject(obj);
	}
	
	/**
	 * 编辑（for dwr）
	 * @param OrderForm
	 */
	public void editOrder(OrderForm obj){
		if(null == obj.getId()){
			this.addOrder(obj);
		}else{
			this.updateOrder(obj);
		}
	}	
	
	/**
	 * 修改
	 * @param OrderForm
	 */
	public void updateOrder(OrderForm obj){
		this.dao.updateObject(obj);
	}
	
	/**
	 * 删除
	 * @param id
	 */
	public void deleteOrder(Long id){
		OrderForm obj = this.dao.loadObject(OrderForm.class, id);
		this.dao.deleteObject(obj);
	}
	
	public void deleteOrderByIds(String ids){
		if(StringUtils.isNotBlank(ids)){
			String []id  = ids.split(",");
			for(String s:id){
				deleteOrder(Long.parseLong(s));
			}			
		}	
	}

	public OrderForm getOrderById(Long id){
		return this.dao.loadObject(OrderForm.class, id);
	}
	
	public OrderForm getOrderById(Long id,boolean lazy){
		if(lazy){
			return this.dao.loadObject(OrderForm.class, id);	
		}else{
			OrderForm obj = this.dao.loadObject(OrderForm.class, id);
			if(obj!=null){
				obj.getFactoryList().size();
				obj.getIncomeList().size();
			}
			return obj;
		}		
	}
	
	/**
	 * 判断订单号重复
	 * @param code
	 * @return
	 */
	public Boolean existsOrderCode(String code){
		OrderForm obj = this.dao.loadObject(OrderForm.class, "orderCode=?",code);
		if(null==obj){
			return false;
		}
		return true;
	}
	
	public List<OrderForm> getOrderFormByIds(String ids){
		if(StringUtils.isNotBlank(ids)){
			return this.dao.loadObjects(OrderForm.class, "id in(" + ids + ")", "id desc", null, null);			
		}
		return null;
	}
	
	public FactoryPayment getFactoryById(Long id){
		return this.dao.loadObject(FactoryPayment.class, id);
	}
	
	public IncomeExpenses getIncomeById(Long id){
		return this.dao.loadObject(IncomeExpenses.class, id);
	}
	
	/**
	 * 添加
	 * @param OrderForm
	 */
	public void addFactory(FactoryPayment obj){
		this.dao.saveObject(obj);
	}
	
	/**
	 * 添加
	 * @param OrderForm
	 */
	public void addIncome(IncomeExpenses obj){
		this.dao.saveObject(obj);
	}
	
	/**
	 * 修改
	 * @param OrderForm
	 */
	public void updateFactory(FactoryPayment obj){
		this.dao.updateObject(obj);
	}
	
	/**
	 * 修改
	 * @param OrderForm
	 */
	public void updateIncome(IncomeExpenses obj){
		this.dao.updateObject(obj);
	}
	
	/**
	 * 编辑（for dwr）
	 * @param OrderForm
	 */
	public void editFactory(FactoryPayment obj){
		if(null == obj.getId()){
			this.addFactory(obj);
		}else{
			this.updateFactory(obj);
		}
	}	
	
	/**
	 * 编辑（for dwr）
	 * @param OrderForm
	 */
	public void editIncome(IncomeExpenses obj){
		if(null == obj.getId()){
			this.addIncome(obj);
		}else{
			this.updateIncome(obj);
		}
	}	
	
	/**
	 * 删除
	 * @param id
	 */
	public void deleteFactory(Long id){
		FactoryPayment obj = this.dao.loadObject(FactoryPayment.class, id);
		this.dao.deleteObject(obj);
	}
	
	/**
	 * 删除
	 * @param id
	 */
	public void deleteIncome(Long id){
		IncomeExpenses obj = this.dao.loadObject(IncomeExpenses.class, id);
		this.dao.deleteObject(obj);
	}
	
	public void deleteFactoryByIds(String ids){
		if(StringUtils.isNotBlank(ids)){
			String []id  = ids.split(",");
			for(String s:id){
				deleteFactory(Long.parseLong(s));
			}			
		}	
	}
	
	public void deleteIncomeByIds(String ids){
		if(StringUtils.isNotBlank(ids)){
			String []id  = ids.split(",");
			for(String s:id){
				deleteIncome(Long.parseLong(s));
			}			
		}	
	}
}
